package org.zsqx.tools;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.EmbeddedObject;
import lotus.domino.NotesException;
import lotus.domino.NotesFactory;
import lotus.domino.RichTextItem;
import lotus.domino.Session;

public class LotusTools {
	//发送灾情上报
	public boolean sendMail(String subject,String fileName,String infosources){
		PropertiesTools pt = new PropertiesTools();
		String noteSendName = null;
		if("月报".equals(infosources) || "".equals(infosources)){
			noteSendName = pt.getPropertiesValue("noteSendName");
		}else if("word".equals(infosources)){
			noteSendName= pt.getPropertiesValue("noteSendWarnName");
		}else{
			noteSendName = pt.getPropertiesValue("noteDirectlySendWarnName");
		}
		return sendLotus(subject, fileName, noteSendName);
	}
	//发送灾情报文
	public boolean sendWarnMail(String subject,String fileName){
		PropertiesTools pt = new PropertiesTools();
		String noteSendName = pt.getPropertiesValue("noteSendWarnName");
		return sendLotus(subject, fileName, noteSendName);
	}
	public boolean sendLotus(String subject,String fileName,String noteSendName){
		PropertiesTools pt = new PropertiesTools();
		String notePassword = pt.getPropertiesValue("notePassword");
//		String noteSendName = pt.getPropertiesValue("noteSendName");
		try {
			Session s = NotesFactory.createSession("172.22.1.90","中山气象台",notePassword);
			Database db = s.getDatabase(s.getServerName(), "mail/中山气象.nsf");
			Document doc = db.createDocument();
			RichTextItem rti = doc.createRichTextItem("Body");
			String name = "xml";
			rti.embedObject(EmbeddedObject.EMBED_ATTACHMENT, null, fileName, name);
			doc.appendItemValue("subject", subject);
			System.out.println("lotus发送到："+noteSendName);
			String[] noteSends = noteSendName.split("[,]");
			for (String note : noteSends) {
				doc.send(false, note);
				System.out.print("发送邮件给"+note+"成功");
			}
			return true;
		} catch (NotesException e) {
			System.out.println("发送邮件出错！");
			e.printStackTrace();
			return false;
		}
	}
	public static void main(String[] args) {
		LotusTools lt = new LotusTools();
	//	lt.sendMail("newTest", "D:/abc.txt");
	}

}
